package com.boboPlanet.user.service.impl;

import com.boboPlanet.comm.component.JwtConfig;
import com.boboPlanet.user.service.SystemAuthService;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description: TODO
 * @Author: mis_wu
 * @Date: 2023/5/10
 * @Company xxx
 **/
@Service
@Slf4j
public class SystemAuthServiceImpl implements SystemAuthService {

    @Resource
    private JwtConfig jwtConfig;

    @Override
    public Boolean getJwtClaimBySign(String sign) {
        if (StringUtils.isBlank(sign)){
            return false;
        }
        Claims claim = jwtConfig.getTokenClaim(sign);
        if (ObjectUtils.isEmpty(claim)){
            return false;
        }
        return true;
    }

    @Override
    public Boolean judgeUserIsExpire(String sign) {
        if (StringUtils.isBlank(sign) || StringUtils.EMPTY.equals(sign)){
            return false;
        }
        Claims tokenClaim = jwtConfig.getTokenClaim(sign);
        if (ObjectUtils.isEmpty(tokenClaim)){
            return false;
        }
        return tokenClaim.getExpiration().before(new Date());
    }
}
